package com.gress.shop.dao.impl;

import com.gress.shop.bo.Member;
import com.gress.shop.bo.example.MemberExample;
import com.gress.shop.dao.IMemberDao;
import com.gress.shop.mapper.MemberMapper;
import com.gress.shop.util.MD5Util;
import com.gress.shop.util.UUIDUtil;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.List;

@Service
public class MemberDaoImpl implements IMemberDao {

    @Resource
    private MemberMapper memberMapper;

    @Override
    public Member insertMember(Member member) {
        member.setId(UUIDUtil.getUUID());
        if (!StringUtils.isEmpty(member.getPassword())) {
            member.setPassword(MD5Util.MD5(member.getPassword()));
        }
        memberMapper.insertSelective(member);
        return member;
    }

    @Override
    public int deleteMemberById(String id) {
        return memberMapper.deleteByPrimaryKey(id);
    }

    @Override
    public boolean updateMemberPSDByEmail(String email, String newPsd) {
        int i = memberMapper.updatePSDByEmail(email, MD5Util.MD5(newPsd));
        if (i != 1) {
            return false;
        }else {
            return true;
        }
    }

    @Override
    public Member updateMember(Member member) {
        memberMapper.updateByPrimaryKeySelective(member);
        return member;
    }

    @Override
    public List<Member> queryMember(MemberExample example) {
        return memberMapper.selectByExample(example);
    }

    @Override
    public Member selectByPrimaryKey(String id) {
        return memberMapper.selectByPrimaryKey(id);
    }
}
